package com.cdn.allmobilestore.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.cdn.allmobilestore.R;
import com.cdn.allmobilestore.ui.BaseFragment;
import com.cdn.allmobilestore.widget.TabIndicator;

public class FragmentPagerAdapter extends PagerAdapter implements View.OnClickListener  {
	private ViewPager mPager;
	private String mCurrentTag = "fragment_tag_category";
	private TabChangedListener mTabChangedListener;
	private final List<TabIndicator> mTabs = new ArrayList<TabIndicator>();
	private FragmentTransaction mCurrentTransaction;
	private FragmentManager mFragmentManager;
	private final List<BaseFragment> mFragments = new ArrayList<BaseFragment>();
	private Context mContext;
	public FragmentPagerAdapter(FragmentManager fm)
	{
		this.mFragmentManager = fm;
		
	}
	
	public void setContext(Context context)
	{
		mContext = context;
	}
	
	public void addFragment(TabIndicator tab, BaseFragment fragment)
	 {
	    this.mFragments.add(fragment);
	    this.mTabs.add(tab);
	    tab.setOnClickListener(this);
	 }
	  
	public void destroyItem(ViewGroup paramViewGroup, int paramInt, Object paramObject)
	  {
	    if (this.mCurrentTransaction == null)
	      this.mCurrentTransaction = this.mFragmentManager.beginTransaction();
	    this.mCurrentTransaction.detach((BaseFragment)paramObject);
	  }

	  public void finishUpdate(ViewGroup paramViewGroup)
	  {
	    if (this.mCurrentTransaction != null)
	    {
	      this.mCurrentTransaction.commitAllowingStateLoss();
	      this.mCurrentTransaction = null;
	      this.mFragmentManager.executePendingTransactions();
	    }
	  }

	  public int getCount()
	  {
		//  Log.d("tabsample","size:" + this.mFragments.size());
	    return this.mFragments.size();
	  }

	  public int getFragmentPosition(BaseFragment fragment)
	  {
		int i=0;  
		for(i=0;i<this.mFragments.size();i++)
		{
			if (TextUtils.equals(fragment.getTag(),((BaseFragment)this.mFragments.get(i)).getTag()))
				return i;			
		}
		return -2;
	
	  }
	  
	 public BaseFragment getItem(int paramInt)
	 {
		 BaseFragment localBaseFragment = null;
		
	    if ((paramInt < 0) || (paramInt >= this.mFragments.size()))
	    	return null;
	    else
	    	localBaseFragment = (BaseFragment)this.mFragments.get(paramInt);
	    
	    return localBaseFragment;
	 }
	 
	 public int getItemPosition(Object fragment)
	  {          
		 int pos = getFragmentPosition((BaseFragment)fragment);
		 if(pos != -2)
			 return pos;
		 else return -1;
	  }

	  public int getPositionByTag(String tag)
	  {

		for(int i =0;i< this.mFragments.size();i++)
		{
			Log.d("tabsample","tag:" + ((BaseFragment)this.mFragments.get(i)).getTag());
			Log.d("tabsample","tag:" + tag);
			if (TextUtils.equals(tag, ((BaseFragment)this.mFragments.get(i)).getTag()))
				return i;
		}		
		return 0;	    
	  }

	  

	  public String getTagByPosition(int paramInt)
	  {
		String str = null;
	    if ((paramInt < 0) || (paramInt > this.mFragments.size()))
	    	return str;
	    else
	    	str = ((BaseFragment)this.mFragments.get(paramInt)).getTag();
	    
	    return str;
	  }

	  public BaseFragment instantiateItem(ViewGroup viewgroup, int position)
	  {
	    if (this.mCurrentTransaction == null)
	      this.mCurrentTransaction = this.mFragmentManager.beginTransaction();
	    BaseFragment localBaseFragment = getItem(position);
	    this.mCurrentTransaction.attach(localBaseFragment);
	    return localBaseFragment;
	  }

	  public boolean isViewFromObject(View view, Object obj)
	  {
	    if (((BaseFragment)obj).getView() == view)
	    	return true;
		return false;
	 
	  }

	  

	  public void replaceFragment(ViewPager viewpager, BaseFragment fragment1, BaseFragment fragment2, String str)
	  {
	    startUpdate(viewpager);
	    if (this.mCurrentTransaction == null)
	      this.mCurrentTransaction = this.mFragmentManager.beginTransaction();
	    int i = getFragmentPosition(fragment1);
	    this.mFragments.set(i, fragment2);
	    this.mCurrentTransaction.remove(fragment1);
	    this.mCurrentTransaction.add(viewpager.getId(), fragment2, str);
	    finishUpdate(viewpager);
	    this.mCurrentTransaction = null;
	    notifyDataSetChanged();
	  }
	
	  public TabIndicator getTab(int paramInt)
	  {		
		TabIndicator localTabIndicator = null;
	    if ((paramInt < 0) || (paramInt >= this.mTabs.size()))
	    	return localTabIndicator;
	    else
	    	localTabIndicator = (TabIndicator)this.mTabs.get(paramInt);	
	    
	      return localTabIndicator;
	  }

	  public int getTabIndex(TabIndicator paramTabIndicator)
	  {
	    return this.mTabs.indexOf(paramTabIndicator);
	  }
	  
	  
	public void onClick(View tab)
	 {	
		int i= 0;  
		for( i=0; i < this.mTabs.size();i++){
			if (this.mTabs.get(i) == (TabIndicator)tab)
			{
				Log.d("tabsample", "Tabindex:"+i);
				this.mTabChangedListener.onTabChanged(i);
				selectTab(i);
			}					
		}		
		     
	  }
	  
	public void selectTab(int tabpos)
	{
		for(int i =0; i < this.mTabs.size();i++){
			if(tabpos == i)
			{
				((TabIndicator) this.mTabs.get(tabpos)).setSelected(true);
				((TabIndicator) this.mTabs.get(tabpos)).setTextColor(mContext.getResources().getColor(R.color.window_background));
			}				
			else
			{
				((TabIndicator) this.mTabs.get(i)).setSelected(false);
				((TabIndicator) this.mTabs.get(i)).setTextColor(mContext.getResources().getColor(R.color.highlight_text_color_inverse));
			}
				
		}
	 }  
	
	
	public void onTabChanged(int page) {
		// TODO Auto-generated method stub
		Log.d("tabsample","currentPage: " + page);
		this.mPager.setCurrentItem(page);
	}
	
	 public void setTabChangedListener(TabChangedListener paramTabChangedListener)
	  {
	    this.mTabChangedListener = paramTabChangedListener;
	  }
	
	public static abstract interface TabChangedListener
	{
	    public abstract void onTabChanged(int paramInt);
	}

}
